import payMentService from  '../services/payMentService';
import { Toast ,Modal} from 'antd-mobile'
const alert = Modal.alert;

export const payMent =(data,callBack)=>{
	return (dispatch, getState)=> {
		Toast.hide();
		Toast.loading("", 60, () => Toast.info("网络错误", 2));
		//2 支付宝  3微信
		payMentService.payMent(data).then(resultData => {
			Toast.hide();
			if (resultData.code == 1000) {
				checkOutCounterPay(resultData.body.chargeorderId,data.payType,callBack)
			} else {
				Toast.info(resultData.msg);
			}
		},()=>{
			Toast.hide(); 
            Toast.info("系统繁忙，请稍后再试");
		});
	}
}

/*
*   支付宝支付
* */
export const checkOutCounterPay =(chargeorderId,payType,callBack)=>{
	let postDat={
		chargeOrderId: chargeorderId,
		payType: payType
	}
	Toast.hide();
	Toast.loading("", 60, () => Toast.info("网络错误", 2));
	payMentService.pay(postDat).then(resultData => {
		Toast.hide();
		if (resultData.code == 1000) {
			if(payType == 3){
				sessionStorage.setItem('weixinPay',1)
				sessionStorage.setItem('forwardUrl',resultData.body.forwardUrl)
				sessionStorage.setItem('wxData',chargeorderId)
			}
			callBack&&callBack(resultData.body.payUrl)
		} else {
			Toast.info(resultData.msg);
		}
	},()=>{
		Toast.hide(); 
		Toast.info("系统繁忙，请稍后再试");
	});
}

/*
 *   微信查询
*   SUCCESS—支付成功
	REFUND—转入退款
	NOTPAY—未支付
	CLOSED—已关闭
	REVOKED—已撤销（刷卡支付）
	USERPAYING--用户支付中
	PAYERROR--支付失败(其他原因，如银行返回失败)
 * */
export const weixinQuery =(data,successUrl,callBack)=>{
	let postDat={
		chargeOrderId:  data
	}
	Toast.hide();
	Toast.loading("", 30, () => Toast.info("网络错误", 2));
	payMentService.weixinQuery(postDat).then(resultData => {
		Toast.hide();
		if (resultData.code == 1000) {  //跳到支付成功页
			if(resultData.body.payStatus == 'SUCCESS'){
				location.href = successUrl
			}
		} else {
			Toast.info(resultData.msg);
		}
	},()=>{
		Toast.hide(); 
		Toast.info("系统繁忙，请稍后再试");
	});
}